package com.wxd.async;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author wxd
 * @version V1.0
 * @description CustonSync
 * @date 2022/10/12 15:33
 **/
@Slf4j
@Component
public class CustomSync {
    /**
     * 无返回值异步线程
     *
     * @throws InterruptedException
     */
    @Async
    public void asyncProcess() throws InterruptedException {
        log.info("async process task, current thread name -> {}", Thread.currentThread().getName());
        //模拟处理任务花费时间
        TimeUnit.SECONDS.sleep(1);
    }

    /**
     * 有返回值异步线程
     *
     * @return
     * @throws InterruptedException
     */
    @Async
    public Future<Integer> asyncProcessHasReturn() throws InterruptedException {
        log.info("async process task (has return), current thread name -> {}", Thread.currentThread().getName());
        //模拟处理任务花费时间
        TimeUnit.SECONDS.sleep(1);
        return new AsyncResult<Integer>(100);
    }

}
